package com.csr.csrcheck.controller;

import com.csr.csrcheck.pojo.Company;
import com.csr.csrcheck.pojo.Evaluate;
import com.csr.csrcheck.pojo.Product;
import com.csr.csrcheck.service.CompanyService;
import com.csr.csrcheck.service.EvaluateService;
import com.csr.csrcheck.service.ProductService;
import com.csr.csrcheck.service.ex.CompanyException;
import com.csr.csrcheck.service.ex.EvaluateException;
import com.csr.csrcheck.util.JsonResult;
import com.csr.csrcheck.util.PageResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RequestMapping({"evaluate"})
@RestController
public class EvaluateController extends BaseController {
    private static final Logger log = LoggerFactory.getLogger(com.csr.csrcheck.controller.EvaluateController.class);

    @Resource
    private EvaluateService evaluateService;

    @Resource
    private CompanyService companyService;

    @Resource
    private ProductService productService;

    @GetMapping({"evaluatelist"})
    public JsonResult<List<Evaluate>> Flight_checklist(HttpServletResponse response) throws IOException, ServletException {
        List<Evaluate> list = this.evaluateService.getEvaluatelist();
        if (list == null)
            throw new EvaluateException("");
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @GetMapping({"evaluate"})
    public JsonResult<Object> evaluate(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize, @RequestParam(value = "cname", required = false) String company_name, @RequestParam(value = "pname", required = false) String product_name) {
        PageResult pageResult = this.evaluateService.getlistpage(pageNum, pageSize, company_name, product_name);
        log.info("evaluate--------------------------->pageNum:" + pageNum);
        log.info("evaluate--------------------------->pageSize:" + pageSize);
        log.info("evaluate--------------------------->company_name:" + company_name);
        log.info("evaluate--------------------------->product_name:" + product_name);
        return new JsonResult(Integer.valueOf(0), "json", pageResult);
    }

    @RequestMapping(path = {"addEvaluate"}, method = {RequestMethod.POST})
    public JsonResult<Void> addEvaluate(Evaluate evaluate) {
        this.evaluateService.addevlaute(evaluate);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"uppdateEvaluate"}, method = {RequestMethod.POST})
    public JsonResult<Void> uppdateEvaluate(Evaluate evaluate) {
        this.evaluateService.updateevlaute(evaluate);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"deleteEvaluate/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Void> deleteEvaluate(@PathVariable("id") int id) {
        this.evaluateService.deleteevlaute(id);
        return new JsonResult(Integer.valueOf(0), "json");
    }

    @RequestMapping(path = {"findEvaluate/{id}"}, method = {RequestMethod.POST})
    public JsonResult<Evaluate> findEvaluate(@PathVariable("id") int id) {
        Evaluate evaluate = this.evaluateService.findevluateByid(id);
        if (evaluate == null)
            throw new CompanyException("");
        return new JsonResult(Integer.valueOf(0), "json", evaluate);
    }

    @RequestMapping(path = {"showcompany"}, method = {RequestMethod.POST})
    public JsonResult<List<Company>> showcompany() {
        List<Company> list = this.companyService.getCommpanylist();
        return new JsonResult(Integer.valueOf(0), "json", list);
    }

    @RequestMapping(path = {"listproduct"}, method = {RequestMethod.POST})
    public JsonResult<List<Product>> listproduct() {
        List list = this.productService.list();
        return new JsonResult(Integer.valueOf(0), "json", list);
    }
}
